/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PV112;

import com.jogamp.opengl.util.gl2.GLUT;
import com.jogamp.opengl.util.texture.Texture;
import java.nio.FloatBuffer;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
/**
 *
 * @author Erik
 */
public class Magnet {
    public static final float MAX_HIGH = 14f, MIN_HIGH = 3f, MIN_DISTANCE = -3f,  MAX_DISTANCE = -19f;
    private boolean turnedOn = false;
    private Point3f position = new Point3f(0f, 12f, -5f), colisionPosition = new Point3f(0f, 12f, -5f);
    private GLUT glut = new GLUT();
    private float rotation = 0f, radius = 2f;
    private OpenGlListener main;
    private Texture activeTexture;
    private Box activeBox;
    
    public void render(GL2 gl){
        gl.glTranslatef(position.x, position.y, position.z);
        activeTexture.bind(gl);
        
        gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_SPHERE_MAP);
        gl.glTexGeni(GL2.GL_T, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_SPHERE_MAP);
        gl.glEnable(GL2.GL_TEXTURE_GEN_S);
        gl.glEnable(GL2.GL_TEXTURE_GEN_T);
        
        FloatBuffer fb = FloatBuffer.allocate(16);
        gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, fb);
        colisionPosition.x = fb.get(12);
        colisionPosition.y = fb.get(13);
        colisionPosition.z = fb.get(14);

        float [] dir = {0f, -1f, 0f};
        float [] lightPos = {0f, MAX_HIGH - colisionPosition.y + 2f, 0f, 1f};
        gl.glLightfv(GL2.GL_LIGHT5, GL2.GL_SPOT_DIRECTION, dir, 0);
        gl.glLightfv(GL2.GL_LIGHT5, GL2.GL_POSITION, lightPos, 0);
        
        float [] cut2 = {colisionPosition.y * 2.5f};
        gl.glLightfv(GL2.GL_LIGHT5, GL2.GL_SPOT_CUTOFF, cut2, 0);
        gl.glLightf(GL2.GL_LIGHT5, GL2.GL_QUADRATIC_ATTENUATION, 1.5f/(32*colisionPosition.y));
        
        gl.glRotatef(90f, 1f, 0f, 0f);
        glut.glutSolidCylinder(.3f, 1f, 32, 32);
        gl.glRotatef(90f, -1f, 0f, 0f);
        main.getRubberTexture().bind(gl);
        gl.glLineWidth(5f);
        gl.glBegin(GL2.GL_LINES);
            gl.glVertex3f(0f, 0f, 0f);
            gl.glVertex3f(0f, MAX_HIGH - position.y + 2.5f, 0f);
        gl.glEnd();
        gl.glDisable(GL2.GL_TEXTURE_GEN_S);
        gl.glDisable(GL2.GL_TEXTURE_GEN_T);
    }
    
    public Point3f getPosition() {
        return position;
    }

    public void setPosition(Point3f position) {
        this.position = position;
    }

    public boolean isTurnedOn() {
        return turnedOn;
    }

    public void setTurnedOn(boolean turnedOn) {
        this.turnedOn = turnedOn;
        if (turnedOn){
            this.activeTexture = main.getToxicTexture();
            main.moveBox(colisionPosition);
        }else{
            this.activeTexture = main.getRubberTexture();
            if (activeBox != null){
                activeBox.animateFall();
            }
        }
    }

    public float getRotation() {
        return rotation;
    }

    public void setRotation(float rotation) {
        this.rotation = rotation;
    }

    public void setMain(OpenGlListener main) {
        this.main = main;
    }
 
    public void writeInfo(){
        System.out.println("Magnet at  : " + position.x + ", " + position.y + ", " + position.z);
        System.out.println("Collides at: " + colisionPosition.x + ", " + colisionPosition.y + ", " + colisionPosition.z);
    }

    public Point3f getColisionPosition() {
        return colisionPosition;
    }

    public Box getActiveBox() {
        return activeBox;
    }

    public void setActiveBox(Box activeBox) {
        this.activeBox = activeBox;
    }

    public float getRadius() {
        return radius;
    }
    
}
